package com.erbao.nettystudy.s03;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author CaoBaoYin
 * @since 2021/10/31 19:58
 */
public class ClientFrame extends Frame {
    public static final ClientFrame INSTANCE = new ClientFrame();
    TextArea ta = new TextArea();// 多行文本
    TextField tf = new TextField();// 单行文本
    private Client client;

    private ClientFrame() throws HeadlessException {
        this.setSize(600, 400);
        this.setLocation(100,20);
        this.add(ta, BorderLayout.CENTER);
        this.add(tf, BorderLayout.SOUTH);
        // 回车调用
        tf.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // ta.setText(ta.getText() + tf.getText());
                client.sendMsg(tf.getText());
                tf.setText("");
            }
        });
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                client.closeConnect();
                System.exit(0);
            }
        });
    }

    public void connectToServer(){
        client = new Client();
        client.connect();
    }


    public void updateFrameTa(String msg) {
        ta.setText(ta.getText().concat(System.getProperty("line.separator").concat(msg)));
    }

    public static void main(String[] args) {
        INSTANCE.setVisible(true);
        INSTANCE.connectToServer();
    }

}
